import {getImportantNotice} from "../../api/notice"
import {findReportOnLimit} from "../../api/report"
import {SocketTask} from "../../utils/socketTask"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuArr:[
      {
        id:1,
        title:"我的邮箱",
        imageUrl:"/assets/icon/menu/通知类.png",
        navigateToUrl:"/pages/menu/inform/index"
      },
      {
        id:2,
        title:"一键上报",
        imageUrl:"/assets/icon/menu/上报问题.png",
        navigateToUrl:"/pages/menu/report/index"
      },
      {
        id:3,
        title:"家庭维修",
        imageUrl:"/assets/icon/menu/Repair.png",
        navigateToUrl:"/pages/menu/repair/index"
      },
      {
        id:4,
        title:"垃圾分类",
        imageUrl:"/assets/icon/menu/垃圾分类查询.png",
        navigateToUrl:"/pages/menu/rubbish/index"
      },
      // {
      //   id:5,
      //   title:"体温监测",
      //   imageUrl:"/assets/icon/menu/体温计.png",
      //   navigateToUrl:"/pages/menu/tiwen/index"
      // },
      {
        id:6,
        title:"在线问诊",
        imageUrl:"/assets/icon/menu/wenzhen.png",
        navigateToUrl:"/pages/menu/wenzhen/index"
      },
      {
        id:7,
        title:"问诊记录",
        imageUrl:"/assets/icon/menu/msg.png",
        navigateToUrl:"/pages/menu/xiaoxi/index"
      },
      {
        id:8,
        title:"更多功能",
        imageUrl:"/assets/icon/menu/more.png",
        navigateToUrl:"/pages/menu/index"
      },
    ],
    swiperArr:[
      {id:"2",src:"/assets/swiper/2.jpeg"},
      {id:"3",src:"/assets/swiper/3.jpeg"},
    ],
    havaNewMsg:false
  },
  async onLoad(){
    SocketTask.onMessage(res=>{
      let {data} = res
      data = JSON.parse(data)
      if(data.haveNewMsg && data.from == "staff"){
        console.log("有新消息待查阅");
        this.setData({
            havaNewMsg:data.haveNewMsg
        })
      }
    })

    let ret = await getImportantNotice()
    let ret2 = await findReportOnLimit(3,0)
    if(ret.code == 200 && ret2.code == 200){
      this.setData({
        importantNotice:ret.data,
        recentReports:ret2.data
      })
    }
  },
  // 控制页面跳转
  navigate(e){
    let navigateToUrl = e.target.id

    wx.navigateTo({
      url: navigateToUrl,
    })
  },
  // 控制点击“更多”时的跳转
  navigateTo(e){
    const {url} = e.currentTarget.dataset

    wx.navigateTo({
      url,
    })
  },
  // 去往通知详情
  toNoticeDetail(e){
    let {id} = e.currentTarget.dataset;
    id = id.trim()
    wx.navigateTo({
      url: '/pages/menu/inform/detail/index?id='+id,
    })
  }
})